<template>
	<view class="agent-update">
		<gyd-navbar back-color="#fff" :title="title"></gyd-navbar>

		<view class="text-prompt">
			<view class="hd">代理升级方式说明：</view>
			<view class="bd">
				<view>方式1：分享用户，免费升高等级。</view>
				<view>方式2：采购价子升级，直接采购提供收货地址电话，直接升级。</view>
				<view>方式3：采购升级礼包升级。</view>
				<view>方式4：补等级差价升级。</view>
				<view>方式5：联系推广员调等级升级。</view>
			</view>
		</view>

		<!--这里要做切换来显示不同表单-->
		<view class="mod-grid">
			<view :class="state.tabIndex == 1 ? 'item active' : 'item'" @click="changeTab(1)">
				<view class="label">自己升级代理</view>
				<view class="value">升级自己的代理等级，需要缴纳费用</view>
			</view>
			<view :class="state.tabIndex == 2 ? 'item active' : 'item'" @click="changeTab(2)">
				<view class="label">下级调代理</view>
				<view class="value">调整下级的代理等级，需扣积分，得相应奖励</view>
			</view>
		</view>

		<u-form class="form" labelPosition="left" :model="formData" :rules="rules" ref="formRef">
			<view v-if="tabIndex == 1">
				<u-form-item class="form-item" label="现有等级" labelWidth="100">
					<u-input border="none" v-model="configInfo.agent.agent_level_name" disabled></u-input>
				</u-form-item>
				<u-form-item class="form-item" label="升级至" labelWidth="100" prop="change_agent_level">
					<uni-data-select v-model="formData.change_agent_level" :localdata="agentLevelList"
						@change="changeAgentLevel()"></uni-data-select>
				</u-form-item>
				<u-form-item class="form-item" label="所需金额" labelWidth="100">
					<u-input border="none" v-model="formData.price" disabled></u-input>
				</u-form-item>
				<view class="tips">选择想要升级的代理级别，系统会计算需要支付的金额</view>
			</view>

			<view v-if="tabIndex == 2">
				<u-form-item class="form-item" label="我的积分" labelWidth="100">
					<u-input border="none" v-model="configInfo.member.points" disabled></u-input>
				</u-form-item>
				<u-form-item class="form-item" label="下级会员" labelWidth="100" prop="member_id">
					<cus-selects :closeSelect.sync="false" :data="memberList" :value="formData.member_id"
						placeholder="请输入关键字搜索" @change="changeMember()" :clearable="true" :filterable="true"
						:searchType='1'></cus-selects>
				</u-form-item>
				<!-- <u-form-item class="form-item" label="下级会员ID" labelWidth="100">
					<u-input border="none" v-model="formData.member_id" placeholder="请输入下级会员ID"></u-input>
				</u-form-item>
				<u-form-item class="form-item" label="下级会员姓名" labelWidth="100">
					<u-input border="none" v-model="formData.nickname" placeholder="输入会员ID后自动获取" disabled></u-input>
				</u-form-item>
				<u-form-item class="form-item" label="下级会员电话" labelWidth="100">
					<u-input border="none" v-model="formData.mobile" placeholder="输入会员ID后自动获取" disabled></u-input>
				</u-form-item> -->
				<u-form-item class="form-item" label="下级现有等级" labelWidth="100">
					<u-input border="none" v-model="formData.agent_level" disabled></u-input>
				</u-form-item>
				<u-form-item class="form-item" label="下级调至" labelWidth="100" prop="change_agent_level">
					<uni-data-select v-model="formData.change_agent_level" :localdata="agentLevelList"
						@change="changeAgentLevel()"></uni-data-select>
				</u-form-item>
				<u-form-item class="form-item" label="扣除积分" labelWidth="100">
					<u-input border="none" v-model="formData.score" disabled></u-input>
				</u-form-item>
				<view class="tips">选择想要调整的代理级别，系统会计算需要扣除的积分</view>
			</view>
		</u-form>

		<view class="footer">
			<view class="btn-group">
				<u-button type="primary" size="large" block @click="saveInfo()">提交</u-button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import request from '@/utils/request';
	import cache from '@/utils/cache';
	import util from '@/utils/util';
	import {
		ref,
		reactive,
		toRefs,
		inject,
	} from 'vue';
	import {
		onLoad,
		onShow,
		onReady,
		onPullDownRefresh,
		onReachBottom,
	} from '@dcloudio/uni-app';
	import cusSelects from '@/components/cus-selects-fan/cus-selects-fan.vue';

	const state = reactive({
		title: '代理升级',
		themeColor: cache.get('themeColor'),
		imgHost: util.requestUrl(),
		tabIndex: 1,
		configInfo: {
			config: {},
			member: {},
			agent: {},
		},
		formData: {
			change_agent_level: '',
			price: '',
			member_id: '',
			score: '',
		}
	});

	const rules = ref([]);
	const formRef = ref();
	const agentLevelList = ref([]);
	const memberList = ref([]);

	//在页面所有元素加载完成后执行（只执行一次）
	onLoad((opt) => {
		console.log('onLoad');
		if (opt.hasOwnProperty('id')) {
			console.log('传递参数', opt.id);
		}
		getConfig();
	});

	//在页面由不可见变为可见时执行（只要页面显示都会执行）
	onShow(() => {
		console.log('onShow');
	})

	//在页面解析完成之后执行
	onReady(() => {
		console.log('onReady');
	})

	onPullDownRefresh(() => {
		console.log('下拉刷新');
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 1000);
	})

	onReachBottom(() => {
		console.log('触底加载更多');
	})

	function getConfig() {
		request.get('agent.uplevel/upConfig').then(res => {
			if (res.code == 1) {
				state.configInfo = res.data
				state.agentLevelList = res.data.levelList
			} else {
				uni.showToast({
					title: res.msg,
					icon: 'none',
					mask: true
				})
			}
		})
	}

	// 获取下级会员
	function getMemberList() {

	}

	// 获取下级会员等级
	function getAgentLevel() {

	}

	function saveInfo() {
		console.log('提交', state.formData);
	}

	function changeAgentLevel(e) {
		console.log('选择等级', e);
	}

	function changeMember(e) {
		console.log('选择下级会员', e);
	}

	function changeTab(val) {
		state.tabIndex = val
		if (val == 2) {
			getMemberList();
		}
	}

	function clearData() {
		state.formData = {
			change_agent_level: '',
			price: '',
			member_id: '',
			score: '',
		};
	}

	function pageRoute(url) {
		uni.navigateTo({
			url
		});
	}

	function pageTabRoute(url) {
		uni.reLaunch({
			url
		});
	}

	const {
		title,
		themeColor,
		imgHost,
		tabIndex,
		configInfo,
		formData,
	} = toRefs(state);
</script>

<style lang="scss">
	page {
		background: linear-gradient(180deg, rgba(199, 224, 255, 0.5) 0%, rgba(199, 224, 255, 0.2) 77%, rgba(255, 255, 255, 0) 100%);
	}

	.agent-update {
		padding-bottom: calc(100rpx + env(safe-area-inset-bottom));

		.text-prompt {
			padding: $m24;

			.hd {
				margin-bottom: 10rpx;
				font-size: 34rpx;
				color: #333;
				font-weight: 700;
			}

			.bd {
				view {
					margin-bottom: 10rpx;
					color: #313131;
				}
			}
		}

		.mod-grid {
			display: flex;
			justify-content: space-between;
			padding: $m24;

			.item {
				flex: 1;
				height: 130rpx;
				padding: $m16;
				background: url($web_url+'/static/images/bg-agent.jpg') no-repeat center;
				background-size: 100% 100%;
				border-radius: 10rpx;

				&:nth-child(1) {
					margin-right: $m24;
				}

				&:nth-child(2) {
					margin-left: $m24;
				}

				&.active {
					border: $warning-color solid 6rpx;
				}

				.label {
					margin-bottom: 10rpx;
					font-size: 32rpx;
					color: #fff;
					font-weight: 600;
				}

				.value {
					color: #ddeffb;
					font-size: 24rpx;
				}
			}
		}

		.form {
			padding: $m24;

			.form-item {
				// align-items: center;
				background: #fff;
				// padding: $m24;
				margin: 0 0 $m16 0;
				border-radius: 10rpx;
			}

			.tips {
				font-size: 24rpx;
				color: $tips-color;
			}
		}

		.footer {
			position: fixed;
			/* 固定在底部 */
			bottom: 0;
			width: 100%;

			.btn-group {}
		}
	}
</style>